<?php
/*
*  Created:  2011-03-05
*  会员数据组件
*  @Author nic <nic-chen@qq.com>
*/
class userCom {

	private $table = 'nic_users';
	/*
	 * 插入或更新一条用户登录数据
     * @param array() $data		用户数据数组
	 * @param int $id		sina_uid
     * @return boolean
	 */
	function insertUser($data, $id = '') {
		if(!is_array($data)) {
			return RST(false, $errno=1210000, $err='Parameter can not be empty');
		}
		
		$this->_cleanCache();
		$db = APP :: ADP('db');
		$db->setTable($this->table);
		$rs = $db->save($data, $id, '');
		return RST($rs);
	}

	/*
	* 根据用户名称获取用户数据
    * @param string $nickname
    * @param int $offset
    * @param int $each
    * @return array()
	*/
	function getByName($name, $ban=0, $offset = 0, $each = 0) {
		if (!is_numeric($offset) || !is_numeric($each)) {
			return RST(false, $errno=1210002, $err='Parameter must be a number');
		}
		
		$db = APP :: ADP('db');
		$keyword = $db->escape($name);

		$where = $limit = "";
		$where_arr = array($name,$ban);
		if($keyword) {
			$where = ' WHERE `name` = "' . $keyword . '" ';
		}
		if($ban)
		{
			if($where)
				$where .= $where . ' and and level<0 ';
			else
				$where = 'where level<0 ';
		}

		if($each) {
			$limit =  ' LIMIT ' . $offset . ',' . $each;
		}
		
		$sql = 'SELECT * FROM ' . $this->table . $where . ' ORDER BY `id` DESC ' . $limit;
		return RST($db->query($sql));
	}

	/**
	* 根据sina_uid称获取用户数据
    * @param int $id
    * @return array()
	*/
	function getByUid($id) {
		if (!is_numeric($id)) {
			return RST(false, $errno=1210002, $err='Parameter must be a number');
		}
	
		$db = APP :: ADP('db');
		$id = $db->escape($id);

		$where = " WHERE `id` = '" . $id . "'"; 

		$sql = 'SELECT * FROM ' . $this->table . $where . ' ORDER BY `id` DESC ';
		return RST($db->getRow($sql));
	}

    /*
     * 用户删除
     * @param int $id
     * @return boolean
     */
	function del($id) {
		if (!is_numeric($id)) {
			return RST(false, $errno=1210002, $err='Parameter must be a number');
		}
		$this->_cleanCache();
		$db = APP :: ADP('db');
		$db->setTable($this->table);
		return RST($db->delete($id));

	}

	/**
	* 根据uid获取用户是否为封禁用户
    * @param int $id
    * @return boolean
	*/
	function getBanById($id) {
		if(empty($uid)) {
			return RST(false, $errno=1210000, $err='Parameter can not be empty');
		}

		if (!is_numeric($id)) {
			return RST(false, $errno=1210002, $err='Parameter must be a number');
		}

		$db = APP :: ADP('db');
		$keyword = $db->escape($id);

		$sql = 'SELECT * FROM ' .$this->table . ' WHERE `id` = ' . $keyword . ' and leval<0 ORDER BY `id` DESC ';
		if($db->getOne($sql)) {
             return RST(true);
		}else{
             return RST(false);
		}
	}

    /*
     * 删除禁封用户
     * @param int $id
     * @return boolean
     */
	function delBan($id) {
		$db = APP :: ADP('db');
		$db->setTable(T_USER_BAN);
		$this->_cleanCache();
		return RST($db->delete($id, '', 'sina_uid'));
	}

	/*
	 * 清除缓存
	 */
	function _cleanCache() {
		DD('mgr/userCom.getByUid');
		DD('mgr/userCom.getByName');
		DD('mgr/userCom.getBySiteUid');
		DD('mgr/userCom.getUseBanById');
		DD('mgr/userCom.getUseBanByName');
		DD('mgr/userCom.getBanByUid');
	}
}
